package home;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Response;

import java.util.ArrayList;
import java.util.List;

import base.BasePage;
import bean.TopBean;
import httptool.QLParser;
import httptool.getHttpUrl;
import httptool.takeUrlResource;
import pagerindicator.TabPageIndicator;
import shanshuitiancheng.news.R;

/**
 * Created by cch on 2016/6/22.
 */
public class SettingPage1 extends BasePage{

    public SettingPage1(Context ct) {
        super(ct);
    }
    private Object json;
    private NewPagerAdapters adapter;
    private TabPageIndicator tpic;
    private ViewPager pager;
    private ArrayList<FunctionPage> pages = new ArrayList<>();
    private int curIndex = 0;


    @Override
    public View initView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.frag_news,null);
        tpic = (TabPageIndicator) view.findViewById(R.id.indicator);
        pager = (ViewPager) view.findViewById(R.id.pager);
        getJson();
        return view;
    }

    @Override
    public void initData() {
        //解析头部数据
        initIndicator();
    }

    private void initIndicator(){
        pages.clear();
        for(int i = 0;i < datas.size();i++){
            String typeurl = datas.get(i).getTypeurl();
            pages.add(new FunctionPage(ct,typeurl));
        }
        adapter = new NewPagerAdapters(ct,pages,datas);
        pager.removeAllViews();
        pager.setAdapter(adapter);

        tpic.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                FunctionPage page = pages.get(position);

                page.initData();
                curIndex = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        pages.get(0).initData();
        tpic.setViewPager(pager);
        tpic.setCurrentItem(curIndex);
    }


    private ArrayList<TopBean.TypesBean> datas =new ArrayList<>();
    public Object getJson() {
        takeUrlResource.getJson(ct, getHttpUrl.TopUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                TopBean data = QLParser.parse(s,TopBean.class);
                int retcode = data.getRetcode();
                if (retcode == 200){
                    List<TopBean.TypesBean> types = data.getTypes();
                    datas.addAll(types);
                }
            }
        });
        return json;
    }

    private class NewPagerAdapters extends PagerAdapter {
        private Context ct;
        private ArrayList<FunctionPage> pages;
        private ArrayList<TopBean.TypesBean> datas;
        public NewPagerAdapters(Context ct, ArrayList<FunctionPage> pages, ArrayList<TopBean.TypesBean> datas) {
            super();
            this.ct = ct;
            this.pages = pages;
            this.datas = datas;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            if (position >= pages.size()) return;
            ((ViewPager)container).removeView(pages.get(position).getRootView());
//            super.destroyItem(container, position, object);
        }

         public CharSequence getPageTtle(int position){
             return datas.get(position).getTypename();
         }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ((ViewPager)container).addView(pages.get(position).getRootView());
            return pages.get(position).getRootView();
        }

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }
}
